package com.jic.manager.impl;

import com.jic.common.base.vo.PageResult;
import com.jic.conf.request.SysNoticeRequest;
import com.jic.manager.entity.SysNotice;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.mapper.SysNoticeMapper;
import com.jic.manager.response.SysNoticeResponse;
import com.jic.manager.service.SysNoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公告记录
 * @author : 
 * @email : 
 * @since : 2020-02-26 07:22:01
 * @version : v1.0.0
 */
@Service
public class SysNoticeServiceImpl implements SysNoticeService{

    @Resource
    private SysNoticeMapper sysNoticeMapper;

    @Override
    public long insert(SysNoticeRequest sysNoticeRequest) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(sysNoticeRequest,sysNotice);
        sysNotice.setCreator(null!=sysNoticeRequest.getAdminOperate()?sysNoticeRequest.getAdminOperate().getOperator():0);
        sysNotice.setCreatorName(null!=sysNoticeRequest.getAdminOperate()?sysNoticeRequest.getAdminOperate().getOperateName():"");
        long flag = sysNoticeMapper.insert(sysNotice);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return sysNoticeMapper.delete(id);
    }

    @Override
    public long update(SysNoticeRequest sysNoticeRequest) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(sysNoticeRequest,sysNotice);
        sysNotice.setUpdater(null!=sysNoticeRequest.getAdminOperate()?sysNoticeRequest.getAdminOperate().getOperator():0);
        sysNotice.setUpdaterName(null!=sysNoticeRequest.getAdminOperate()?sysNoticeRequest.getAdminOperate().getOperateName():"");
        return sysNoticeMapper.update(sysNotice);
    }

    @Override
    public SysNotice queryByPrimaryKey(Long id){
        return sysNoticeMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<SysNotice> query(SysNotice sysNotice){
        return sysNoticeMapper.query(sysNotice);
    }

    @Override
    public PageResult<SysNotice> queryPage(SysNoticeRequest sysNoticeRequest) {
        //计算下标
        int startIndex = (sysNoticeRequest.getStart() - 1) * sysNoticeRequest.getLimit();
        List<SysNotice> list = sysNoticeMapper.queryPage(startIndex, sysNoticeRequest.getLimit(), sysNoticeRequest);
        long count = sysNoticeMapper.queryPageCount(sysNoticeRequest);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public List<SysNoticeResponse> getTimeSort() {
        return sysNoticeMapper.getTimeSort();
    }

    @Override
    public Integer setaudit(SysNoticeRequest request) {
        if (request.getId() == null && request.getAuditStatus() == null){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"传入参数为空");
        }
        Integer set = sysNoticeMapper.setaudit(request);
        if (set != 1){
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"审核失败");
        }
        return 1;
    }


}